package demo1;

import java.util.Collection;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 邓大帅
 * Date: 2024-01-17
 * Time: 18:02
 */
public class Sort {
    /**
     * 插入排序
     * 时间复杂度：O(N^2) 最好：O(N) 数据基本有序时，建议用插入排序
     * 空间复杂度：O(1)
     * 稳定性:稳定
     * @param array
     */
    public static void insertSort(int[] array) {
        int j = 0;
        for (int i = 1; i < array.length; i++) {
            j = i-1;
            int tmp = array[i];
            while (j >= 0) {
                if (tmp < array[j]) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
                j--;
            }
            array[j+1] = tmp;
        }
    }

    /**
     * 希尔排序
     * 时间复杂度：无法分析出 大概O(N^1.3 ~ N^1.5)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * @param array
     */
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array,gap);
        }
        shell(array,1);
    }
    public static void shell(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            while (j > 0) {
                if (tmp < array[j]) {
                    array[j+gap] = array[j];
                }else {
                    break;
                }
                j -= gap;
            }
            array[j+gap] = tmp;
        }
    }
    /**
     * 选择排序
     * 时间复杂度：O(N^2)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     */
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int max = 0;
            for (int j = 1; j < array.length - i; j++) {
                if (array[j] > array[max]) {
                    max = j;
                }
            }
            int tmp = array[max];
            array[max] = array[array.length - i - 1];
            array[array.length - i - 1] = tmp;
        }
    }
    /**
     * 堆排序
     * 时间复杂度：O(N*logN)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     */
    public static void swap(int[] array, int a, int b) {
        int tmp = array[a];
        array[a] = array[b];
        array[b] = tmp;
    }

    public static void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length - 1;
        while (end > 0) {
            swap(array,0,end);
            shiftDown(array,0,end);
            end--;
        }
    }
    public static void createBigHeap(int[] array) {
        for (int parent = (array.length - 2) / 2; parent >= 0; parent--) {
            shiftDown(array,parent,array.length);
        }
    }
    public static void shiftDown(int[] array, int parent, int len) {
        int child = parent*2 + 1;
        while (child < len) {
            if (child + 1 < len && array[child + 1] > array[child]) {
                child++;
            }
            if (array[child] > array[parent]) {
                swap(array,child,parent);
                parent = child;
                child = parent*2 + 1;
            }else {
                break;
            }
        }
    }
    /**
     * 冒泡排序
     * 时间复杂度：O(N^2)
     * 空间复杂度：O(1)
     * 稳定性：稳定
     */
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length-i-1; j++) {
                if (array[j] > array[j+1]) {
                    swap(array,j,j+1);
                    flag = true;
                }
            }
            if (!flag) return;
        }
    }
    /**
     * 快速排序
     * 时间复杂度：O(N*logN) 最坏O(N^2) 但是可以通过有坏避免最坏时间复杂度
     * 空间复杂度：最好：O(logN) 最坏：O(N)
     * 稳定性：不稳定
     */
    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }
    private static void quick(int[] array, int left, int right) {
        if (left >= right) {
            return;
        }
        //减少递归次数 这个优化不一定有用，很鸡肋
        if (left - right <= 10) {
            //插入排序
            return;
        }

        //三数取中法
        int mid = midThree(array,left,right);
        swap(array,left,mid);

        int pivot = partition(array,left,right);
        quick(array,left,pivot-1);
        quick(array,pivot+1,right);
    }
    private static int midThree(int[] array, int left, int right) {
        int mid = (right + left) / 2;
        if (array[left] < array[right]) {
            if (array[mid] < array[left]) {
                return left;
            }else if(array[mid] > array[right]) {
                return right;
            }else {
                return mid;
            }
        }else {
            if (array[mid] < array[right]) {
                return right;
            }else if(array[mid] > array[left]) {
                return left;
            }else {
                return mid;
            }
        }
    }
    //挖坑法
    private static int partition(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp ) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }
    //Hoare法
    private static int partition1(int[] array, int left, int right) {
        int tmp = array[left];
        int i = left;
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp ) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,i);
        return left;
    }
    //前后指针法 了解即可
    private static int partition2(int[] array, int left, int right) {
        int prev = left;
        int cur = left + 1;
        while (cur <= right) {
            if (array[cur] < array[left] && array[++prev] != array[cur] ) {
                swap(array,prev,cur);
            }
            cur++;
        }
        swap(array,prev,left);
        return prev;
    }
    /**
     * 非递归实现快排
     * 用栈实现
     */
    public static void quickSort1(int[] array) {
        Deque<Integer> stack = new LinkedList<>();
        int left = 0;
        int right = array.length - 1;
        int pivot = partition(array,left,right);
        if (pivot > left + 1) {
            stack.push(left);
            stack.push(pivot - 1);
        }
        if (pivot < right - 1) {
            stack.push(pivot + 1);
            stack.push(right);
        }
        while (!stack.isEmpty()) {
            int right1 = stack.pop();
            int left1 = stack.pop();
            pivot = partition(array,left,right);
            if (pivot > left + 1) {
                stack.push(left);
                stack.push(pivot - 1);
            }
            if (pivot < right - 1) {
                stack.push(pivot + 1);
                stack.push(right);
            }
        }
    }
    /**
     * 归并排序
     * 时间复杂度：O(N*logN)
     * 空间复杂度：O(N)
     * 稳定性：稳定
     * 稳定的排序 归并 冒泡 插入
     */
    public static void mergeSort(int[] array) {
        mergeSortFunc(array,0,array.length - 1);
    }
    private static void mergeSortFunc(int[] array, int left, int right) {
        if (left >= right) {
            return;
        }
        int mid = (left + right) / 2;
        mergeSortFunc(array,left,mid);
        mergeSortFunc(array,mid+1,right);
        merge(array,left,right,mid);
    }
    private static void merge(int[] array, int start, int end, int mid) {
        int s1 = start;
        int s2 = mid + 1;
        int[] tmp = new int[end - start + 1];
        int k = 0;
        while (s1 <= mid && s2 <= end) {
            if (array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= mid) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= end) {
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < tmp.length; i++) {
            array[start+i] = tmp[i];
        }
    }
    /**
     * 非递归实现归并排序
     */
    public static void mergeSort1(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i += gap*2) {
                int left = i;
                int mid = i + gap - 1;
                if (mid >= array.length) {
                    mid = array.length - 1;
                }
                int right = mid + gap;
                if (right >= array.length) {
                    right = array.length - 1;
                }
                merge(array,left,right,mid);
            }
            gap *= 2;
        }
    }
}
